package com.appbee.songs.client.ui.entry;

import javax.inject.Inject;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.FlowPanelDropController;
import com.appbee.core.client.listener.SimpleSelectionListener;
import com.appbee.gdata.youtube.client.model.SimpleEntry;
import com.appbee.gdata.youtube.client.search.Entry;
import com.appbee.gdata.youtube.client.search.MediaContent;
import com.appbee.gdata.youtube.client.search.MediaGroup;
import com.appbee.gdata.youtube.client.search.MediaThumbnail;
import com.appbee.gdata.youtube.client.ui.IYoutubeStyle;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.editor.client.EditorDelegate;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.editor.client.ValueAwareEditor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class EntryListView extends Composite implements
		ValueAwareEditor<String>, LeafValueEditor<String>
{
	FlowPanel panel = new FlowPanel();
	private final IYoutubeStyle youtubeStyle;
	private SimpleEntry selectedEntry;
	private SimpleSelectionListener<SimpleEntry> selectionListener;
	private final PickupDragController dragController;
	private final FlowPanelDropController dropController;

	@Inject
	public EntryListView(final IYoutubeStyle youtubeStyle)
	{
		this.youtubeStyle = youtubeStyle;
		dropController = new FlowPanelDropController(panel)
		{
			@Override
			protected Widget newPositioner(DragContext context)
			{
				Label positioner = new Label();
				positioner.addStyleName(youtubeStyle.posistioner());
				return positioner;
			}

		};
		dragController = new PickupDragController(RootPanel.get(), false);
		dragController.registerDropController(dropController);
		initWidget(panel);
	}

	@Override
	public String getValue()
	{
		StringBuffer value = new StringBuffer();
		value.append("[");
		int length = panel.getWidgetCount();
		for (int i = 0; i < length; i++)
		{
			SimpleEntry entry = ((EntryView) panel.getWidget(i)).getEntry();
			if (i > 0)
				value.append(",");
			value.append(new JSONObject(entry).toString());
		}
		value.append("]");

		GWT.log("getValue: " + value.toString());
		return value.toString();
	}

	@Override
	public void setDelegate(EditorDelegate<String> delegate)
	{
	}

	@Override
	public void flush()
	{
	}

	@Override
	public void onPropertyChange(String... paths)
	{
	}

	@Override
	public void setValue(String value)
	{
		GWT.log("setValue: " + value);
		panel.clear();
		if (value != null)
		{
			JsArray<SimpleEntry> entryArray = SimpleEntry.parseJson("v="
					+ value);
			for (int i = 0; i < entryArray.length(); i++)
			{
				SimpleEntry entry = entryArray.get(i);
				panel.add(createEntryView(entry));
			}
		}
	}

	public SimpleEntry getSelectedEntry()
	{
		return selectedEntry;
	}

	public void setSelectionListener(SimpleSelectionListener<SimpleEntry> selectionListener)
	{
		this.selectionListener = selectionListener;
	}

	public void add(Entry entry)
	{
		SimpleEntry e = SimpleEntry
				.create2(entry.getId(), entry.getTitle(), "");
		MediaGroup mediaGroup = entry.getMediaGroup();
		if (mediaGroup != null && mediaGroup.getMediaThumbnail() != null
				&& mediaGroup.getMediaThumbnail().length() > 0)
		{
			MediaThumbnail mediaThumbnail = mediaGroup.getMediaThumbnail()
					.get(0);
			e.setThumbnail(mediaThumbnail.getUrl());
			JsArray<MediaContent> mediaContent = mediaGroup.getMediaContent();
			if (mediaContent.length() > 0)
			{
				e.setDuration(mediaContent.get(0).getDuration());
			}
		}

		panel.add(createEntryView(e));

	}

	private EntryView createEntryView(final SimpleEntry e)
	{
		final EntryView entryView = new EntryView(e, youtubeStyle);
		dragController.makeDraggable(entryView);
		entryView.getRemoveButton().addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				panel.remove(entryView);

			}
		});
		ClickHandler clickHandler = new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				selectedEntry = e;
				selectionListener.onSelected(selectedEntry);
			}
		};
		entryView.thumbnail.addClickHandler(clickHandler);
		entryView.text.addClickHandler(clickHandler);
		return entryView;
	}

	public void clear()
	{
		panel.clear();
	}

	public void selectFirstEntry()
	{
		EntryView entryView = (EntryView) panel.getWidget(0);
		selectedEntry = entryView.getEntry();
		selectionListener.onSelected(selectedEntry);
	}

}
